/*
 * @Descripttion:
 * @version: 1.0.0
 * @Author: YinJianFeng
 * @Date: 2024-03-29 16:24:47
 */
// import { BaseCommand } from 'nextcax-app'
import { wsApi } from '@app-cad/server'
// import { CMEVENTS, userEvents } from 'nextcax-events'
// import { Picker } from 'nextcax-picker'
import {
  BaseCommand,
  CMEVENTS
} from '@amcax/base'
import {
  userEvents
} from '@app-cad/module';
import {
  PickerMgr,
  Picker3D,
} from '@amcax/renderer';

let isDeleting = false;
export const deleteModels = async () => {
  if (isDeleting) return
  const model_dxid = Array.from(PickerMgr.getPicker<Picker3D>('3d').getSelectedModels().keys())
  if (model_dxid.length) {
    isDeleting = true;
    wsApi.delete(model_dxid);
  }
};

const isDerleted = () => {
  isDeleting = false;
};

export const cmdDelete: BaseCommand = {
  id: 'CmdDelete',
  icon: 'commands/feature/delete',
  label: 'command.delete',
  noHighlight: true,

  invoke: deleteModels,

  initialDisabled: true,
  subscribeDisabled: (setDisabled: (disabled: boolean) => void) => {
    const listener = (can: boolean) => {
      setDisabled(!can);
    };
    userEvents
      .on(CMEVENTS.CAN_DELETE, listener)
      .on(CMEVENTS.MODEL_REMOVED, isDerleted);
    return () => userEvents.off(CMEVENTS.CAN_DELETE, listener);
  },
};
